import axios from "axios";

let baseURL = "https://gitcode.net/rubickcenter/rubick-database/-/raw/master";
let access_token = "";

try {
  const dbdata = window.rubick.db.get("rubick-localhost-config");
  baseURL = dbdata.data.database;
  access_token = dbdata.data.access_token;
} catch (e) {
  // ignore
}

const instance = axios.create({
  baseURL: baseURL || "https://gitcode.net/rubickcenter/rubick-database/-/raw/master",
});

export default {
  async getTotalPlugins() {
    let targetPath = "plugins/total-plugins.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },

  async getFinderDetail() {
    let targetPath = "plugins/finder.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },

  async getSystemDetail() {
    let targetPath = "plugins/system.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },
  async getWorkerDetail() {
    let targetPath = "plugins/worker.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },

  async getPluginDetail(url: string) {
    const res = await axios.get(url);
    return res.data;
  },

  async getSearchDetail() {
    let targetPath = "plugins/search.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },
  async getDevDetail() {
    let targetPath = "plugins/dev.json";
    if (access_token) {
      targetPath = `${encodeURIComponent(targetPath)}/raw?access_token=${access_token}&ref=master`
    }
    const res = await instance.get(targetPath);
    return res.data;
  },
};
